/**
* @Author: 858834013@qq.com
* @Name: rememberPwd
* @Date: 2022-07-09
* @Desc: 记住密码
*/
<template>
	<div class="rememberPwd">
		<u-checkbox-group @change="change">
			<u-checkbox :checked="rememberPwd" shape="circle" label="记住密码"></u-checkbox>
		</u-checkbox-group>
	</div>
</template>

<script>
	export default {
		name: "rememberPwd",
		components: {},
		props: {
			password: {
				type: String,
				default () {
					return '';
				}
			},
			username: {
				type: String,
				default () {
					return '';
				}
			},
		},
		data() {
			return {
				rememberPwd: false
			}
		},
		watch: {
			rememberPwd() {
				uni.setStorageSync('rememberPwd', this.rememberPwd)
			}
		},
		mounted() {
			const username = uni.getStorageSync('username');
			const password = uni.getStorageSync('password');
			const rememberPwd = uni.getStorageSync('rememberPwd');
			if (rememberPwd) {
				this.rememberPwd = true
			} else {
				this.rememberPwd = false
			}
			if (username && password) {
				this.$emit('update:username', username)
				this.$emit('update:password', password)
			} else {
				this.$emit('update:username', '')
				this.$emit('update:password', '')
			}
		},
		methods: {
			change() {
				this.rememberPwd = !this.rememberPwd
			},
			rememberPassword() {
				if (this.rememberPwd) {
					uni.setStorageSync('username', this.username);
					uni.setStorageSync('password', this.password);
				} else {
					uni.removeStorageSync('username');
					uni.removeStorageSync('password');
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.rememberPwd {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: nowrap;
		flex-direction: row;
		align-content: flex-start;
	}
</style>
